home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue28 / survive / Phonetic / QUERY.SQL < prev   
Encoding:
Text File  |  1997-10-22  |  1.1 KB  |  41 lines

  1. drop procedure MakeSoundex
  2. go
  3. create procedure MakeSoundex(@Word varchar(255), @Code varchar(4) output)
  4. as
  5.   declare @I Int
  6.   declare @Ch Char
  7.   declare @LastCh Char
  8.   declare @MaxCodeLength SmallInt
  9.   declare @LetterCodes char(26)
  10. begin
  11.   if @Word is null begin
  12.     select @Code = null
  13.     return(0)
  14.   end
  15.                        /*ABCDEFGHIJKLMNOPQRSTUVWXYZ*/
  16.   select @LetterCodes = '01230120022455012623010202'
  17.   select @MaxCodeLength = 4
  18.   select @Code = Char(0), @LastCh = Char(0), @I = 1
  19.   select @Word = Upper(@Word)
  20.  
  21.   while DataLength(@Code) <> @MaxCodeLength
  22.     begin
  23.       if @I > DataLength(@Word)
  24.         select @Code = @Code + '0'
  25.       else begin
  26.         select @Ch = Substring(@Word, @I, 1)
  27.         if (@Ch >= 'A') and (@Ch <= 'Z')
  28.           if @Code = Char(0)
  29.             select @Code = @Ch
  30.           else begin
  31.             select @Ch = Substring(@LetterCodes, Ascii(@Ch) - 64, 1)
  32.             if (@Ch <> '0') and (@Ch <> @LastCh)
  33.               select @Code = @Code + @Ch, @LastCh = @Ch
  34.           end
  35.         select @I = @I + 1
  36.       end
  37.     end
  38. end
  39. go
  40.  
  41.